package com.jcark.ui.view.recyclerview.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Description:自定义的LinearLayoutManager <br>
 * 修正Invalid view holder adapter positionBaseViewHolder异常
 *
 * @author King.Zi<br>
 * @date 2022/12/1<br>
 * Email: 78688689@qq.com
 */
public class MLinearLayoutManager extends androidx.recyclerview.widget.LinearLayoutManager {
    public static final String TAG = "LinearLayoutManager";

    private boolean mCanVerticalScroll = true;

    public MLinearLayoutManager(Context context) {
        super(context);
    }

    public MLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public MLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (Exception e) {
            Log.e(TAG, "on layout children has exception the message is " + e.getMessage());
        }
    }

    @Override
    public void collectAdjacentPrefetchPositions(int dx, int dy, RecyclerView.State state,
                                                 LayoutPrefetchRegistry layoutPrefetchRegistry) {
        try {
            super.collectAdjacentPrefetchPositions(dx, dy, state, layoutPrefetchRegistry);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "collect Adjacent Prefetch Positions has exception the message is " + e.getMessage());
        }
    }

    @Override
    public boolean canScrollVertically() {
        if (!mCanVerticalScroll) {
            return false;
        } else {
            return super.canScrollVertically();
        }
    }

    public void setCanVerticalScroll(boolean b) {
        mCanVerticalScroll = b;
    }
}
